/**
 * 
 */
package org.braketester.model;

import java.io.IOException;
import java.nio.DoubleBuffer;
import java.util.ArrayList;

import org.braketester.entry.ExceptionResolver;


/**
 * @author LAP
 *
 */
public class RequestThread extends Thread {
	private RawData data;
	private LTR212ThreadRequestedDevice device;
	
	public RequestThread(LTR212ThreadRequestedDevice device, int channelsCount) {
		super();
		if (device == null) throw new IllegalArgumentException("device must be not null");
		this.device = device;
		data = new RawData(channelsCount);
	}
	
	@Override
	public void run() {
		while(true) {
			data.addDataList(tryGetData());
		}
	}
	
	private ArrayList<Double> tryGetData() {
		DoubleBuffer doubleBuffer = null;
		try {
			doubleBuffer = device.getData();
		} catch (ClassNotFoundException | IOException e) {
			ExceptionResolver.getInstance().logException(e);
			ExceptionResolver.getInstance().showException(e);
		}
		ArrayList<Double> ret = new ArrayList<Double>(doubleBuffer.capacity());
		for(Double dbl : doubleBuffer.array()) ret.add(dbl);
		return ret;
	}
	
	public RawData getRawData() {
		return data;
	}
}
